var webpackMerge = require('webpack-merge');
var ExtractTextPlugin = require('extract-text-webpack-plugin');
var webpack = require('webpack');
var CopyWebpackPlugin = require('copy-webpack-plugin');
var TypedocWebpackPlugin = require('typedoc-webpack-plugin');
var commonsPlugin = new webpack.optimize.CommonsChunkPlugin('common.js');
var HtmlWebpackPlugin = require('html-webpack-plugin');

var commonConfig = require('./webpack.common.js');
var utils = require('./utils');
const ENV = process.env.ENV = process.env.NODE_ENV = 'development';

module.exports = webpackMerge(commonConfig({ env: ENV }), {
    entry: entries(utils.root('src') + '/*'),
    devtool: 'cheap-module-source-map', //for  debug ts file,'eval'
    devServer: {
        historyApiFallback: true,
        stats: 'minimal'
    },
    output: {
        path: utils.root('dist'),
        publicPath: '', // relative path  <>
        filename: 'js/[name].bundle.js',
        chunkFilename: 'js/[id].chunk.bundle.js',
        //    library: 'lib'
    },

    plugins: [
        new webpack.BannerPlugin('Owned by WIT'),

        //commonsPlugin,

        new webpack.optimize.CommonsChunkPlugin({ //common file
            name: ['ZZpolyfills', 'XXapp'].reverse()
        }),

        new HtmlWebpackPlugin({
            template: 'src/index.html'
        }),

        // new HtmlWebpackPlugin({
        //     filename: 'notfound.html',
        //     template: './src/notfound.html'
        // }),

        new CopyWebpackPlugin([
            {
                from: './src/lib/lib.js',
                to: 'js/lib.js',
                force: true
            },
            {
                from: './src/app.config.js',
                force: true
            },
            {
                from: './src/config',
                to: 'config',
                force: true
            }
        ]),

        new ExtractTextPlugin("assets/[name].css"),

        new webpack.ContextReplacementPlugin(            
            /angular(\\|\/)core(\\|\/)(esm(\\|\/)src|src)(\\|\/)linker/,
            utils.root('./src'), 
            {}
        ),

        new webpack.DllReferencePlugin({
            context: __dirname,
            manifest: require('../src/lib/manifest.json'),
        }),

        // new TypedocWebpackPlugin({
        //     name: 'WIT',
        //     mode: 'file',
        //     includeDeclarations: false,
        //     ignoreCompilerErrors: true,
        // })
    ]

});

function entries(globPath) {
    var entries = {
        'ZZpolyfills': './src/polyfills.ts',
        'XXapp': './src/main.ts',
        'ZZstyle': './src/assets/lib.scss',
        'boot': './src/assets/boot.scss',

    };
    //utils.getEntries(globPath, entries);
    return entries;
}

